package com.supermap.dataHandling.utils;

/**
 * Created by bruce on 18/8/23 -- 17:50
 */

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.springframework.core.io.ClassPathResource;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;

/**
 * @author bruce
 * @date 18/8/23 17:50
 * @Description: 描述--文件的工具类
 */
public class FileTool {
    //最终结果的目录
    public static String etl = "F:\\zhgl\\etl";
    public static String etlFile = "F:\\zhgl\\etlFile";

    /**
     * @author bruce
     * @date 18/8/23 17:50
     * @Description: 描述--获取resources下的etl文件
     */
    public static File getEtlRoot() throws IOException {
        return new ClassPathResource("etl").getFile();
    }

    /**
     * 获取传过来的file下的csv和xls文件
     *
     * @param fileStr
     */
    private static Collection getXLS_CSV(String fileStr) {
        File file = new File(fileStr);
        Collection<File> collection = FileUtils.listFiles(file, FileFilterUtils.suffixFileFilter(".xls"), null);
        Collection<File> listFiles1 = FileUtils.listFiles(file, FileFilterUtils.suffixFileFilter(".csv"), null);
        collection.addAll(listFiles1);
        return collection;
    }

    public static void main(String[] args) throws IOException {
//        copyAllFile(new File("D:\\SuperMapBruceWorkSpace\\etlWorkSpace\\文档和数据\\联网直报数据计算(7月)"), resultDest);
//        getXLS_CSV(resultDest.getPath());
//        String filePath = "D:\\Result\\20180008-2018_09_04_18_11_51";
//        deleteZip(filePath);
//        copyAllFile(filePath, filePath);
//        copyFile2Directory("D:\\test.txt", "D:\\Result\\etl");
//        File srcFile = new File("D:\\Result\\test");
//        HashMap<String, String> fileMap = new HashMap<>();
////        批发零售
//        fileMap.put(srcFile.getPath() + File.separator + "e_a.xls", "p_a.xls");
//        fileMap.put(srcFile.getPath() + File.separator + "e_b.csv", "p_b.csv");
////服务业
//        fileMap.put(srcFile.getPath() + File.separator + "f_a.xls", "fw_a.csv");
//        fileMap.put(srcFile.getPath() + File.separator + "f_b.csv", "fw_b.csv");
////房地产
//        fileMap.put(srcFile.getPath() + File.separator + "x_a.xls", "fd_a.xls");
//        fileMap.put(srcFile.getPath() + File.separator + "x_b.csv", "fd_b.csv");
////住宿餐饮
//        fileMap.put(srcFile.getPath() + File.separator + "s_a.xls", "z_a.xls");
//        fileMap.put(srcFile.getPath() + File.separator + "s_b.csv", "z_b.csv");
////
//        FileTool.reNameFile(fileMap);
    }

    public static void deleteZip(String filePath) {
        File file = new File(filePath);
        if (file.isDirectory()) {
            Collection<File> listFiles = FileUtils.listFiles(file, FileFilterUtils.suffixFileFilter(".zip"), null);
            Iterator<File> iterator = listFiles.iterator();
            while (iterator.hasNext()) {
                File deleteFile = iterator.next();
                try {
                    FileUtils.forceDelete(deleteFile);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("删除zip文件成功");
        }
    }

    /**
     * 复制文件夹到文件夹
     */
    public static void copyFile2Directory(String srcDir, String destDir) throws IOException {
        FileUtils.copyFileToDirectory(new File(srcDir), new File(destDir));
    }

    /**
     * 完全复制文件夹到一个新的地方
     */
    public static void copyAllFile(File srcDir, File destDir) throws IOException {
        /**
         * 拷贝整个文件夹到新的文件夹,如果新文件夹不存在自动创建
         */
        FileUtils.copyDirectoryToDirectory(srcDir, destDir);
    }

    /**
     * 完全复制文件夹到一个新的地方
     */
    public static void copyAllFile(String srcDir, String destDir) throws IOException {
        /**
         * 拷贝整个文件夹到新的文件夹,如果新文件夹不存在自动创建
         */
        FileUtils.copyDirectoryToDirectory(new File(srcDir), new File(destDir));
    }

    /**
     * 重命名文件
     *
     * @param fileMap
     */
    public static void reNameFile(HashMap<String, String> fileMap) {
        for (String key : fileMap.keySet()) {
            String value = fileMap.get(key);
            try {
                renameFile(key, value);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void renameFile(String fs, String name) throws Exception {
        File file = new File(fs);
        if (!file.exists()) {
            return;
        }
        System.out.println("修改前文件名称是：" + file.getName());
        String rootPath = file.getParent();
        System.out.println("根路径是：" + rootPath);
        File newFile = new File(rootPath + File.separator + name);
        System.out.println("修改后文件名称是：" + newFile.getName());
        if (file.renameTo(newFile)) {
            System.out.println("修改成功!");
        } else {
            System.out.println("修改失败");
        }
    }

    /**
     * 获取文件夹里面的文件列表
     *
     * @param dir
     */
    public static List<File> getRootList(String dir) {
        File file = new File(dir);
        if (!file.exists() || !file.isDirectory()) {
            return null;
        }
        File[] tempList = file.listFiles();
        if (tempList == null) {
            return null;
        }
        List<File> fileList = new ArrayList<>();
        for (int i = 0; i < tempList.length; i++) {
            if (tempList[i].isDirectory()) {
                fileList.add(tempList[i]);
            }
        }

        return fileList;
    }

    /**
     * 获取指定路径下所有文件的绝对路径
     * @param dir
     * @return
     */
    public static Map<Object,String> getRootListFileName(String dir){
        Map<Object,String> files = new HashMap<>();
        File file = new File(dir);
        File[] tempList = file.listFiles();
        if (tempList == null){
            return null;
        }
        for (int i = 0; i<tempList.length; i++){
            if (tempList[i].isDirectory()){
                System.out.println("文件夹：" + tempList[i]);
            }
            if (tempList[i].isFile()){
                files.put(tempList[i].getName(),tempList[i].toString());
                System.out.println("顺序:" + tempList[i].getName() + i);
            }
        }
        return files;
    }

    public static File getZip(String dir) {
        File src = new File(dir);
        File destFile = new File(src.getParent(), src.getName() + ".zip");
        new ZipUtil(destFile).zipFiles(src);
        return destFile;
    }

    /**
     * 核查文件是否存在
     *
     * @param srcFile
     * @param fileMap
     * @return
     */
    public static Boolean checkFile(File srcFile, HashMap<String, String> fileMap) {
        for (String key : fileMap.keySet()) {
            String value = fileMap.get(key);
            File file = new File(key);
            //如果文件不存在，那就从etl文件夹复制一个过去
            try {
//                File etlFile = ResourceUtils.getFile("classpath:etl");
                if (!file.exists()) {
                    File copyFile = new File(FileTool.etl, value);
                    File file1 = new File(srcFile.getPath() + File.separator + value);
                    if (!file1.exists()){
                        FileUtils.copyFileToDirectory(copyFile, srcFile);
                        System.out.println("复制成功>>>" + srcFile.getName());
                    }
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }
}
